/*
 * $Id: Command.h,v 1.6 2007/06/21 10:49:01 baconevi Exp $
 *
   Copyright 2005-2009 Chung, Hyung-Hwan.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an  BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */

#ifndef _COMMAND_H_
#define _COMMAND_H_

#include <xp/bas/string.h>
#include <xp/bas/stdio.h>

#define CMD_OK      XP_TEXT("OK")
#define CMD_ERR     XP_TEXT("ERR")
#define CMD_INFO    XP_TEXT("INFO")
#define CMD_WARN    XP_TEXT("WARN")

#define CMD_DISCON  XP_TEXT("DISCON")   // disconnect
#define CMD_ADMATH  XP_TEXT("ADMATH")   // administrator authentication
#define CMD_SHTDWN  XP_TEXT("SHTDWN")
#define CMD_ENVGET  XP_TEXT("ENVGET")
#define CMD_ENVSET  XP_TEXT("ENVSET")
#define CMD_ENVLST  XP_TEXT("ENVLST")
#define CMD_ENVITM  XP_TEXT("ENVITM")
#define CMD_LOGCLR  XP_TEXT("LOGCLR")
#define CMD_LOGLST  XP_TEXT("LOGLST")
#define CMD_LOGITM  XP_TEXT("LOGITM")

#define SEND_CMD_0(cmd) \
	do { \
		XP_LOG1 (&app_log, this->name(), XP_LOG_DEBUG, XP_T("<R>%s"), cmd); \
		if (sendCmd(cmd,0) == -1) return -1; \
	} while (0)

#define SEND_CMD_1(cmd,msg) \
	do { \
		XP_LOG2 (&app_log, this->name(), XP_LOG_DEBUG, XP_T("<R>%s %s"), cmd, msg); \
		if (sendCmd(cmd,1,msg) == -1) return -1; \
	} while (0)

#define SEND_CMD_2(cmd,msg1,msg2) \
	do { \
		XP_LOG3 (&app_log, this->name(), XP_LOG_DEBUG, XP_T("<R>%s %s, %s"), cmd, msg1, msg2); \
		if (sendCmd(cmd,2,msg1,msg2) == -1) return -1; \
	} while (0)

#define SEND_CMD_3(cmd,msg1,msg2,msg3) \
	do { \
		XP_LOG4 (&app_log, this->name(), XP_LOG_DEBUG, XP_T("<R>%s %s, %s, %s"), cmd, msg1, msg2, msg3); \
		if (sendCmd(cmd,3,msg1,msg2,msg3) == -1) return -1; \
	} while (0)

#define SEND_CMD_4(cmd,msg1,msg2,msg3,msg4) \
	do { \
		XP_LOG5 (&app_log, this->name(), XP_LOG_DEBUG, XP_T("<R>%s %s, %s, %s, %s"), cmd, msg1, msg2, msg3, msg4); \
		if (sendCmd(cmd,4,msg1,msg2,msg3,msg4) == -1) return -1; \
	} while (0)

#define SEND_CMD_5(cmd,msg1,msg2,msg3,msg4,msg5) \
	do { \
		XP_LOG6 (&app_log, this->name(), XP_LOG_DEBUG, XP_T("<R>%s %s, %s, %s, %s, %s"), cmd, msg1, msg2, msg3, msg4, msg5); \
		if (sendCmd(cmd,5,msg1,msg2,msg3,msg4,msg5) == -1) return -1; \
	} while (0)

#define SEND_CMD_6(cmd,msg1,msg2,msg3,msg4,msg5,msg6) \
	do { \
		XP_LOG7 (&app_log, this->name(), XP_LOG_DEBUG, XP_T("<R>%s %s, %s, %s, %s, %s, %s"), cmd, msg1, msg2, msg3, msg4, msg5, msg6); \
		if (sendCmd(cmd,6,msg1,msg2,msg3,msg4,msg5,msg6) == -1) return -1; \
	} while (0)

#define SEND_CMD_7(cmd,msg1,msg2,msg3,msg4,msg5,msg6,msg7) \
	do { \
		XP_LOG8 (&app_log, this->name(), XP_LOG_DEBUG, XP_T("<R>%s %s, %s, %s, %s, %s, %s, %s"), cmd, msg1, msg2, msg3, msg4, msg5, msg6, msg7); \
		if (sendCmd(cmd,7,msg1,msg2,msg3,msg4,msg5,msg6,msg7) == -1) return -1; \
	} while (0)

#define SEND_CMD_OK           SEND_CMD_0(CMD_OK)
#define SEND_CMD_ERR          SEND_CMD_0(CMD_ERR)
#define SEND_OK(msg)          SEND_CMD_1(CMD_OK,msg)
#define SEND_OK_2(msg1,msg2)  SEND_CMD_2(CMD_OK,msg1,msg2)

#define SEND_ERR(code) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_ERR,__buf__,this->errorStr()); \
	} while (0)

#define SEND_ERR_2(code,msg) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_ERR,__buf__,msg); \
	} while (0)

#define SEND_INFO(code) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_INFO,__buf__,this->errorStr()); \
	} while (0)

#define SEND_INFO_2(code,msg) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_INFO,__buf__,msg); \
	} while (0)

#define SEND_WARN(code) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_WARN,__buf__,this->errorStr()); \
	} while (0)

#define SEND_WARN_2(code,msg) \
	do { \
		setErrorCode (code); \
		xp_char_t __buf__[100]; \
		xp_sprintf (__buf__, xp_countof(__buf__), XP_TEXT("%d"), code); \
		SEND_CMD_2(CMD_WARN,__buf__,msg); \
	} while (0)

#endif
